package com.sample.springbatch.job.demo.service;


import com.sample.springbatch.common.MyException;
import com.sample.springbatch.scheduling.service.QuartzMgrService;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * Created by zwg on 2017/9/13.
 */
@Service
public class BatchDemoService {
    public static final Logger logger = LoggerFactory.getLogger(BatchDemoService.class);

    @Autowired
    QuartzMgrService quartzMgrService;

    /**
     * 启动检查
     */
    public void startCheck() throws MyException{
        int random = new Random().nextInt(5);
        if(random!=1){
            throw new MyException("batch demo start check fail.");
        }else{
            logger.info("batch demo start check ok.");
        }
    }

    /**
     * 批量前初始化数据
     */
    public void initData() throws SchedulerException {
        //记录状态
        quartzMgrService.startQuartz(1);
        //重新加载
        quartzMgrService.loadQuartzJobs(1,true);

    }

    /**
     * 批量结束
     */
    public void batchEnd(){

    }
}
